package com.gsma.calendar.android.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.LayerDrawable;
import android.view.Gravity;
import android.widget.Button;

import com.gsma.calendar.android.R;
import com.gsma.calendar.android.util.UiUtilites;

/**
 * Custom view that represents a {@link Sessions#SESSION_ID} instance, including
 * its title and time span that it occupies. Usually organized automatically by
 * {@link SessionsLayout} to match up against a {@link TimeRulerView} instance.
 */
public class SessionView extends Button {
	private final int mSessionId;
	private final String mTitle;
	private final long mStartTime;
	private final long mEndTime;
	private final boolean mContainsStarred;
	private final int mColumn;
	private final int mColumns;
	private final boolean mBreak;

	public SessionView(Context context, int sessionId, String title, long startTime, long endTime, boolean containsStarred, int column, int columns,
			boolean isBreak) {
		super(context);

		mSessionId = sessionId;
		mTitle = title;
		mStartTime = startTime;
		mEndTime = endTime;
		mContainsStarred = containsStarred;
		mColumn = column;
		mColumns = columns;
		mBreak = isBreak;

		Resources res = getResources();
		setText(mTitle);

		// TODO: turn into color state list with layers?
		int textColor = Color.DKGRAY;
		int accentColor = -1;
		switch (mColumn) {
		/*
		 * case 1: case 4: accentColor = res.getColor(R.color.session_column_2);
		 * break; case 2: case 5: accentColor =
		 * res.getColor(R.color.session_column_3); break;
		 */
		default:
			accentColor = res.getColor(R.color.session_break);
			break;
		}
		if (isBreak) {
			accentColor = res.getColor(R.color.light_grey);// Color.LTGRAY;
															// //res.getColor(R.color.grey);
		}

		LayerDrawable buttonDrawable = (LayerDrawable) res.getDrawable(R.drawable.btn_session);
		buttonDrawable.getDrawable(0).setColorFilter(accentColor, PorterDuff.Mode.SRC_ATOP);
		// buttonDrawable.getDrawable(0).setAlpha(200);
		buttonDrawable.getDrawable(1).setAlpha(mContainsStarred ? 255 : 0);

		setTextColor(textColor);
		setBackgroundDrawable(buttonDrawable);
		setTextSize(res.getDimension(R.dimen.text_size_medium));
		setGravity(Gravity.FILL);
		setTypeface(UiUtilites.getTypeface(context));
	}

	public int getSessionId() {
		return mSessionId;
	}

	public long getStartTime() {
		return mStartTime;
	}

	public long getEndTime() {
		return mEndTime;
	}

	public int getColumn() {
		return mColumn;
	}

	public int getColumns() {
		return mColumns;
	}

	public boolean isBreak() {
		return mBreak;
	}

}
